package com.gcph.zhuoba.main.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.util.L;
import com.gcph.zhuoba.util.PicLoadUtil;
import com.gcph.zhuoba.util.TouchUtil;
/**
 * 球桌页面
 */
public class BallJiasaiView extends SurfaceView implements SurfaceHolder.Callback{
	//定义画笔
	private Paint paint; 		
	//定义画布
	private Canvas canvas = null; 
	private SurfaceHolder sfh = null;
	
	//获取视图的宽度
	private int screenW = 0;
	//获取视图的高度
	private int screenH = 0;
	private int imgW = 0;
	private Bitmap bitmap =null;
	private float fitx =0;
	
	//半径
	private float radius = 0;
	
	public static float jiasaiX=0f;
	public static float jiasaiY=0f;
	
	public BallJiasaiView(Context context,AttributeSet attrs){
		super(context);
		initPaint();
		sfh = getHolder();
		sfh.addCallback(this);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
		//获取视图的宽度
		screenW = getWidth();
		//获取视图的高度
		screenH = getHeight();
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ball_jiasai);
		imgW = bitmap.getWidth();
		
		L.d("screenW="+screenW);
		L.d("screenH="+screenH);
		L.d("imgW="+imgW);
		
		fitx=(screenW-screenH)/2f;
		
		radius = screenH/2f;
		myDraw();
	}
	
	/**
	 * 初始化画笔
	 */
	private void initPaint(){
		paint = new Paint();
		//设置消除锯齿
		paint.setAntiAlias(true);
	}
	
	 public void myDraw() {
		//获取canvas实例
		canvas = sfh.lockCanvas();
		canvas.drawColor(getResources().getColor(R.color.common_background));
		//canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.common_background),new Rect(0,0,71,71), new Rect(0,0,screenW,screenH), paint);
		canvas.drawBitmap(PicLoadUtil.scaleToFit(bitmap,(screenW-2*fitx)/imgW+0.0f),fitx, 0, paint);
		sfh.unlockCanvasAndPost(canvas);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		L.d( "surfaceChanged");
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		L.d( "surfaceDestroyed");
	}
	
	/*
	 * 备注：次处一定要将return super.onTouchEvent(event)修改为return true，原因是：
	 * 1）父类的onTouchEvent(event)方法可能没有做任何处理，但是返回了false。
	 * 2)一旦返回false，在该方法中再也不会收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int cx=0,cy=0;
		cx = (int) event.getX();
		cy = (int) event.getY();
	
		
		if(TouchUtil.isPointInCircle(cx, cy, screenW/2f, radius,radius*4/5))
		{
			paint.setColor(Color.RED);
			//获取canvas实例
			canvas = sfh.lockCanvas();
			
			//画红圈
			canvas.drawColor(getResources().getColor(R.color.common_background));
			canvas.drawBitmap(PicLoadUtil.scaleToFit(bitmap,(screenW-2*fitx)/imgW+0.0f),fitx, 0, paint);
			canvas.drawCircle(cx, cy, screenH/10, paint);
			
			//画白圈
			paint.setColor(Color.WHITE);
			canvas.drawCircle(cx, cy, screenH/10*14/25.0f, paint);
			sfh.unlockCanvasAndPost(canvas);
			
			jiasaiX = (cx-screenW/2f)/radius;
			jiasaiY = (cy-radius)/radius;
			L.d("jiasaiX===="+jiasaiX);
			L.d("jiasaiY===="+jiasaiY);
		}
		return true;  
	}
}